package cn.jbolt.core.handler.base;

import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

import javax.servlet.http.HttpServletRequest;
/**
 * saas模式租户SN 从header提交中获取
 * @ClassName:  JBoltSaasTenantSnHeaderParser
 * @author: JFinal学院-小木 QQ：909854136
 * @date:   2022年2月20日
 *
 */
public class JBoltSaasTenantSnHeaderParser implements JBoltSaasTenantSnParser {
	private String tenantSnKey;
	private boolean isFail;

	public JBoltSaasTenantSnHeaderParser() {
		this.tenantSnKey = DEFAULT_TENANT_SN_KEY;
	}
	public JBoltSaasTenantSnHeaderParser(String tenantSnKey) {
		this.tenantSnKey = tenantSnKey;
	}
	@Override
	public String parse(Invocation inv) {
		return parse(inv.getController());
	}

	@Override
	public String parse(Controller ctrl) {
		return parse(ctrl.getRequest());
	}

	@Override
	public String parse(HttpServletRequest request) {
		String tenantSn = request.getHeader(tenantSnKey);
		if(StrKit.isBlank(tenantSn)) {
			isFail = true;
			return null;
		}
		return tenantSn;
	}
	@Override
	public boolean isFail() {
		return isFail;
	}

}
